﻿namespace WMS6.Web.TransportManagement
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using WMS6.Web;
    using WMS6.Web.TransportMgtServiceRef;
    using WMS6.Web.UserControls;

    public class TrackingList : BasePage
    {
        protected Button btnEdit;
        protected Button btnExport;
        protected Button btnList;
        protected Button btnNew;
        protected Button btnReSearch;
        protected Button btnSearch;
        protected HtmlForm F8042;
        protected GridView gvTrackingDetail;
        protected HtmlInputHidden hdnActionID;
        protected HtmlInputHidden hdnFrom;
        protected HtmlInputHidden hdnRtnValue;
        protected Label lblMsg;
        protected Label lblUom;
        protected HtmlLink mycss;
        protected Pager PagerFooter;
        protected ScriptManager ScriptManager1;
        protected UpdatePanel up1;

        private void BindData2TrackingDetailList(TrackingDetailQueryEntity trackingDetailQuery)
        {
            this.PagerFooter.CurrentPage = int.Parse(trackingDetailQuery.CurrentPage);
            DataSet ds = new DataSet();
            ds = TransportMgtServiceClientProxy.GetInstance().GetTrackingInfoByQueryEntity(trackingDetailQuery);
            base.BindData2GridView(this.gvTrackingDetail, ds);
            if ((ds != null) && (ds.Tables.Count > 1))
            {
                this.PagerFooter.PageInfo = ds.Tables[1].Rows[0]["CurrentPage"].ToString() + " / " + ds.Tables[1].Rows[0]["TotalPagesCount"].ToString() + "&nbsp;&nbsp;&nbsp;共: " + ds.Tables[1].Rows[0]["TotalRecordsCount"].ToString();
                this.PagerFooter.TotalPages = int.Parse(ds.Tables[1].Rows[0]["TotalPagesCount"].ToString());
            }
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            try
            {
                List<string> selectedID = base.GetSelectedIDsInGridView(this.gvTrackingDetail, "TRACKING_ID");
                if ((selectedID == null) || (selectedID.Count == 0))
                {
                    this.lblMsg.Text = base.GetGlobalMsgResource("C008");
                }
                else
                {
                    this.lblMsg.Text = TransportMgtServiceClientProxy.GetInstance().DeleteTrackingDetail(selectedID) + base.GetGlobalMsgResource("C006");
                    TrackingDetailQueryEntity TrackingDetailQuery = this.GetTrackingDetailQueryEntity();
                    this.BindData2TrackingDetailList(TrackingDetailQuery);
                }
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("C007") + ex.Message;
            }
        }

        protected void btnEdit_Click(object sender, EventArgs e)
        {
            List<string> selectedID = base.GetSelectedIDsInGridView(this.gvTrackingDetail, "BOL_ID");
            if ((selectedID == null) || (selectedID.Count == 0))
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("C008");
            }
            else if (selectedID.Count > 1)
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("C009");
            }
            else
            {
                base.Response.Redirect(string.Format("Tracking.aspx?from=list&ID={0}", base.UrlEncode(selectedID[0])));
            }
        }

        protected void btnExport_Click(object sender, EventArgs e)
        {
            TrackingDetailQueryEntity trackingDetailQuery = this.GetTrackingDetailQueryEntity();
            trackingDetailQuery.IsGetAll = true;
            this.hdnActionID.Value = "Y";
            string currentPageIndex = trackingDetailQuery.CurrentPage;
            trackingDetailQuery.CurrentPage = "0";
            this.gvTrackingDetail.Columns[1].Visible = false;
            this.gvTrackingDetail.AllowSorting = false;
            this.BindData2TrackingDetailList(trackingDetailQuery);
            this.hdnActionID.Value = "";
            this.gvTrackingDetail.ShowFooter = false;
            trackingDetailQuery.IsGetAll = false;
            trackingDetailQuery.CurrentPage = currentPageIndex;
            this.SetTrackingQueryEntity(trackingDetailQuery);
            base.Export2Excel("TrackingDetail.xls", this.gvTrackingDetail);
        }

        protected void btnFirst_Click(object sender, EventArgs e)
        {
            TrackingDetailQueryEntity trackingDetailQuery = this.GetTrackingDetailQueryEntity();
            trackingDetailQuery.CurrentPage = "0";
            this.BindData2TrackingDetailList(trackingDetailQuery);
        }

        protected void btnLast_Click(object sender, EventArgs e)
        {
            TrackingDetailQueryEntity trackingDetailQuery = this.GetTrackingDetailQueryEntity();
            trackingDetailQuery.CurrentPage = (this.PagerFooter.TotalPages - 1).ToString();
            this.BindData2TrackingDetailList(trackingDetailQuery);
        }

        protected void btnList_Click(object sender, EventArgs e)
        {
            TrackingDetailQueryEntity trackingDetailQuery = this.GetTrackingDetailQueryEntity();
            trackingDetailQuery.TrackingID = "";
            trackingDetailQuery.BolID = "";
            trackingDetailQuery.DeliveryOrderID = "";
            trackingDetailQuery.EquipmentLicence = "";
            trackingDetailQuery.CarrierID = "";
            trackingDetailQuery.DriverID = "";
            trackingDetailQuery.DriverPhone = "";
            trackingDetailQuery.TrackingDate = "";
            trackingDetailQuery.CreatedBy = "";
            trackingDetailQuery.Status = "";
            this.BindData2TrackingDetailList(trackingDetailQuery);
        }

        protected void btnNext_Click(object sender, EventArgs e)
        {
            TrackingDetailQueryEntity trackingDetailQuery = this.GetTrackingDetailQueryEntity();
            if (int.Parse(trackingDetailQuery.CurrentPage) < (this.PagerFooter.TotalPages - 1))
            {
                trackingDetailQuery.CurrentPage = (int.Parse(trackingDetailQuery.CurrentPage) + 1).ToString();
                this.BindData2TrackingDetailList(trackingDetailQuery);
            }
        }

        protected void btnPrevious_Click(object sender, EventArgs e)
        {
            TrackingDetailQueryEntity trackingDetailQuery = this.GetTrackingDetailQueryEntity();
            if (int.Parse(trackingDetailQuery.CurrentPage) > 0)
            {
                trackingDetailQuery.CurrentPage = (int.Parse(trackingDetailQuery.CurrentPage) - 1).ToString();
                this.BindData2TrackingDetailList(trackingDetailQuery);
            }
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            base.Server.Transfer("TrackingQuery.aspx");
        }

        private TrackingDetailQueryEntity GetTrackingDetailQueryEntity()
        {
            if (!string.IsNullOrEmpty(this.hdnActionID.Value))
            {
                return (base.PageQueryEntityViewState as TrackingDetailQueryEntity);
            }
            return (base.PageQueryEntitySession as TrackingDetailQueryEntity);
        }

        protected void GoTo_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(((TextBox) this.PagerFooter.FindControl("txtGo2Page")).Text.Trim()))
            {
                TrackingDetailQueryEntity trackingDetailQuery = this.GetTrackingDetailQueryEntity();
                trackingDetailQuery.CurrentPage = this.PagerFooter.GoToPage.ToString();
                this.BindData2TrackingDetailList(trackingDetailQuery);
            }
        }

        protected void gvTrackingDetail_Init(object sender, EventArgs e)
        {
            base.InitDetailGridView("F8042", this.gvTrackingDetail);
        }

        protected void gvTrackingDetail_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Cells[0].Text = (((this.PagerFooter.CurrentPage * this.PagerFooter.PageSize) + e.Row.RowIndex) + 1).ToString();
                e.Row.Attributes.Add("onmouseout", "GridViewRowMouseout(this);");
                e.Row.Attributes.Add("onmouseover", "GridViewRowMouseover(this);");
                ((CheckBox) e.Row.FindControl("chbSelect")).Attributes.Add("onclick", "GridViewCheckBoxClick('" + e.Row.ClientID + "')");
                if (!string.IsNullOrEmpty(this.hdnActionID.Value))
                {
                    ((HyperLink) e.Row.FindControl("HyperLink1")).Enabled = false;
                }
            }
        }

        protected void gvTrackingDetail_Sorting(object sender, GridViewSortEventArgs e)
        {
            try
            {
                TrackingDetailQueryEntity trackingDetailQuery = this.GetTrackingDetailQueryEntity();
                trackingDetailQuery.SortField = e.SortExpression;
                if (trackingDetailQuery.SortDirection == "ASC")
                {
                    trackingDetailQuery.SortDirection = "DESC";
                }
                else if (trackingDetailQuery.SortDirection == "DESC")
                {
                    trackingDetailQuery.SortDirection = "ASC";
                }
                this.BindData2TrackingDetailList(trackingDetailQuery);
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = ex.Message;
            }
        }

        private void LoadData()
        {
            TrackingDetailQueryEntity trackingDetailQuery = new TrackingDetailQueryEntity();
            if (string.IsNullOrEmpty(base.Request.QueryString["from"]))
            {
                if (this.PreviousPage != null)
                {
                    trackingDetailQuery.TrackingID = "";
                    trackingDetailQuery.BolID = this.PreviousPage.BolID;
                    trackingDetailQuery.DeliveryOrderID = this.PreviousPage.DeliveryOrderID;
                    trackingDetailQuery.EquipmentLicence = this.PreviousPage.EquipmentLicence;
                    trackingDetailQuery.CarrierID = this.PreviousPage.CarrierID;
                    trackingDetailQuery.DriverID = this.PreviousPage.DriverID;
                    trackingDetailQuery.DriverPhone = this.PreviousPage.DriverPhone;
                    trackingDetailQuery.TrackingDate = this.PreviousPage.TrackingDate;
                    trackingDetailQuery.CreatedBy = this.PreviousPage.CreateBy;
                    trackingDetailQuery.Status = this.PreviousPage.Status;
                }
            }
            else
            {
                this.hdnFrom.Value = base.Request.QueryString["from"].ToString();
            }
            trackingDetailQuery.IsGetAll = false;
            trackingDetailQuery.CurrentPage = "0";
            trackingDetailQuery.PageSize = this.gvTrackingDetail.PageSize.ToString();
            trackingDetailQuery.SortField = "b.ROWID";
            trackingDetailQuery.SortDirection = "DESC";
            this.PagerFooter.PageSize = this.gvTrackingDetail.PageSize;
            if (!string.IsNullOrEmpty(base.Request.QueryString["action"]))
            {
                this.hdnActionID.Value = base.Request.QueryString["action"].ToString();
                this.gvTrackingDetail.Columns[1].Visible = false;
                this.gvTrackingDetail.Columns[2].Visible = true;
                this.btnNew.Visible = false;
                this.btnEdit.Visible = false;
                this.btnReSearch.Visible = false;
                this.btnExport.Visible = false;
            }
            else if (!string.IsNullOrEmpty(base.Request.QueryString["export"]))
            {
                this.hdnActionID.Value = base.Request.QueryString["export"];
            }
            this.SetTrackingQueryEntity(trackingDetailQuery);
            this.BindData2TrackingDetailList(trackingDetailQuery);
            if (!string.IsNullOrEmpty(base.Request.QueryString["export"]))
            {
                this.btnExport_Click(null, null);
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            this.lblMsg.Text = "";
            if (!base.IsPostBack)
            {
                base.InitPage(this.Page);
                this.LoadData();
            }
        }

        private void SetTrackingQueryEntity(TrackingDetailQueryEntity trackingDetailQuery)
        {
            if (!string.IsNullOrEmpty(this.hdnActionID.Value))
            {
                base.PageQueryEntityViewState = trackingDetailQuery;
            }
            else
            {
                base.PageQueryEntitySession = trackingDetailQuery;
            }
        }

        public TrackingQuery PreviousPage
        {
            get
            {
                return (TrackingQuery) base.PreviousPage;
            }
        }
    }
}

